package org.dreamwork.base.sh.service.impl;

import org.dreamwork.base.sh.service.IGenericService;
import org.dreamwork.base.sh.Parameter;
import org.dreamwork.base.sh.Sort;
import org.dreamwork.base.sh.dao.IGenericDao;
import org.dreamwork.util.IDataCollection;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-8-25
 * Time: 13:03:18
 */
public class GenericServiceSpringImpl<T, PK extends Serializable> implements IGenericService<T, PK> {
    protected IGenericDao<T, PK> dao;

    public IGenericDao<T, PK> getDao () {
        return dao;
    }

    public void setDao (IGenericDao<T, PK> dao) {
        this.dao = dao;
    }

    public PK save (T o) {
        return dao.save (o);
    }

    public void save (T... o) {
        dao.save (o);
    }

    public void save (Collection<T> o) {
        dao.save (o);
    }

    public void update (T o) {
        dao.update (o);
    }

    public void update (T... o) {
        dao.update (o);
    }

    public void update (Collection<T> o) {
        dao.update (o);
    }

    public void delete (PK pk) {
        T t = dao.get (pk);
        delete (t);
    }

    public void delete (T o) {
        dao.delete (o);
    }

    public void delete (T... o) {
        dao.delete (o);
    }

    public void delete (Collection<T> o) {
        dao.delete (o);
    }

    public T get (PK id) {
        return dao.get (id);
    }

    public T get (Parameter... parameters) {
        return dao.get (parameters);
    }

    public T get (Collection<Parameter> parameters) {
        return get (toParameterArray(parameters));
    }

    public boolean exists (PK id) {
        return dao.exists (id);
    }

    public boolean exists (Parameter... parameters) {
        return dao.exists (parameters);
    }

    public boolean exists (Collection<Parameter> parameters) {
        return exists (toParameterArray(parameters));
    }

    public List<T> find (Parameter... parameters) {
        return dao.find (parameters);
    }

    public List<T> find (Collection<Parameter> parameters) {
        return find (toParameterArray(parameters));
    }

    public IDataCollection<T> find (int pageNo, int pageSize, Sort sort, Parameter... parameters) {
        return dao.find (pageNo, pageSize, sort, parameters);
    }

    public IDataCollection<T> find (int pageNo, int pageSize, Sort sort, Collection<Parameter> parameters) {
        return find (pageNo, pageSize, sort, toParameterArray(parameters));
    }

    private Parameter[] toParameterArray (Collection<Parameter> coll) {
        Parameter[] array = new Parameter[coll.size ()];
        return coll.toArray (array);
    }
}